package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.hosp.respository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author luoyin
 * @Date 16:01 2022/9/27
 **/
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    DepartmentRepository departmentRepository;

    @Override
    public Page<Department> getDepartmentPage(Map<String, String> paramMap) {
        //查询条件
        Department department = new Department();
        department.setHoscode(paramMap.get("hoscode"));
        department.setIsDeleted(0);
        Example<Department> example = Example.of(department);
        //分页信息
        //第一页从0开始
        PageRequest pageRequest = PageRequest.of(Integer.parseInt(paramMap.get("page"))-1 , Integer.parseInt(paramMap.get("limit")));

        return departmentRepository.findAll(example, pageRequest);
    }

    @Override
    public void save(Map<String, String> paramMap) {
        Department department = JSONObject.parseObject(JSONObject.toJSONString(paramMap), Department.class);
        Department dep = new Department();
        dep.setHoscode(department.getHoscode());
        dep.setDepcode(department.getDepcode());
        Example<Department> example =Example.of(dep);
        Optional<Department> optional = departmentRepository.findOne(example);
        if(optional.isPresent()){
            //存在该科室
            Department op = optional.get();
            department.setId(op.getId());
            department.setCreateTime(op.getCreateTime());
            department.setUpdateTime(new Date());
            department.setIsDeleted(op.getIsDeleted());

        }else {
            //不存在
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
        }
        departmentRepository.save(department);
    }

    @Override
    public void remove(String hoscode, String depcode) {
        Department dep = new Department();
        dep.setHoscode(hoscode);
        dep.setDepcode(depcode);
        Example<Department> example =Example.of(dep);
        Optional<Department> optional = departmentRepository.findOne(example);
        if (!optional.isPresent()){
            throw new YyghException(201,"不存在该科室");
        }
        Department department = optional.get();
        department.setIsDeleted(1);
        departmentRepository.save(department);
    }

    //查询科室及他的子节点
    @Override
    public List<DepartmentVo> getDeptByHoscode(String hoscode) {
        Department dep = new Department();
        dep.setHoscode(hoscode);
        Example<Department> example = Example.of(dep);
        List<Department> all = departmentRepository.findAll(example);
        //   String -> bigcode   List<Department> -> child
        Map<String, List<Department>> map = all.stream().collect(Collectors.groupingBy(Department::getBigcode));

        ArrayList<DepartmentVo> list = new ArrayList<>();
        ArrayList<String> exist = new ArrayList<>();
        for (Map.Entry<String, List<Department>> entry : map.entrySet()) {
            List<Department> value = entry.getValue();
            String bigcode = entry.getKey();
            if( !exist.contains(bigcode)){
                //封装大科室
                DepartmentVo departmentVo = new DepartmentVo();
                departmentVo.setDepcode(bigcode);
                departmentVo.setDepname(value.get(0).getBigname());
                //是否有子节点

                List<DepartmentVo> children = hasChildren(bigcode,map,exist);
                departmentVo.setChildren(children);
                list.add(departmentVo);
            }
        }
        return list;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }

    private List<DepartmentVo> hasChildren(String code,Map<String,List<Department>> map,ArrayList<String> exist){
        //有子节点
        if(map.containsKey(code) && !exist.contains(code)){

            //定义子节点列表
            ArrayList<DepartmentVo> children = new ArrayList<>();
            //获取子节点数据
            List<Department> value = map.get(code);
            exist.add(code);
            //遍历子节点数据,把子节点封装到列表中
            for (Department department : value) {
                DepartmentVo vo = new DepartmentVo();
                vo.setDepname(department.getDepname());
                vo.setDepcode(department.getDepcode());
                //判断子节点是否有子节点
                vo.setChildren(hasChildren(department.getDepcode(),map,exist));
                children.add(vo);
            }
            return children;
        }
        return null;

    }
}
